package com.action.mvc.auth.controller;

import com.action.mvc.auth.common.AjaxResult;
import com.action.mvc.auth.entity.User;
import com.action.mvc.auth.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by Wuxiang on 2017/4/24.
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/index")
    public String index() {
        return "user_list";
    }

    @RequestMapping(value = "/addEditUser",method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult addEditUser(User user){
        if(null == user.getId()){
            userService.addUser(user);
        }else{
            userService.updateUser(user);
        }
        return AjaxResult.success();
    }

    @RequestMapping("/deleteUser")
    @ResponseBody
    public AjaxResult deleteUser(Long id){
        userService.deleteById(id);
        return AjaxResult.success();
    }

    @RequestMapping("/getUsers")
    @ResponseBody
    public List<User> getUsers(Integer page, Integer size){
        return userService.getUsers(page, size);
    }

}
